<?php include("siteTop.php"); ?>

<!--    
    Created By : Julian Ponce
    B.S. Computer Science - De La Salle Canlubang
    November 15, 2011
    Description :
    
    This page allows the admin to view the document details of a particular documentSetupId.
-->
    
<?php 
   
    // THIS SERVES AS THE TEMPORARY DOCUMENT SETUP ID.
    $documentSetupId = $_POST['documentSetupId'];
   
    //CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // IDENTIFIES THE DOCUMENT DETAILS
    $query = 
        "
            SELECT *
            FROM DocumentSetup
            WHERE documentSetupId = '$documentSetupId'
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    
    $documentType = $row['type'];
    $documentName = $row['documentName'];
    $documentDescription = $row['description'];
    
    
?>
<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>

    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){
            
            // [START] BACK BUTTON CLICKED > REDIRECT TO admDocumentSetup.php
            jQuery("#backButton").click(function() {
                    window.location = 'admDocumentSetup.php';                
            });
            
            // [START] AJAX IN CHECKING IF THIS DOCUMENT CAN BE EDITABLE.
            // THIS MEANS THAT THE DOCUMENT HAS ON GOING TRANSACTIONS.
            jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/admCheckDocument.php',
                dataType : 'json',
                async : false,
                data: {
                    documentSetupId : "<?php echo $documentSetupId; ?>",
                    docType : "<?php echo $documentType; ?>"
                },
                success : function(data){
                   
                   // IF DATA/"1" = DOCUMENT HAS ON GOING TRANSACTIONS
                   if (data) {
                       jQuery("#editButton").hide();
                       jQuery("#cannotEditMsg").show();
                   }
                   
                   // ELSE = DOCUMENT HAS NO ON GOING TRANSACTIONS, IT CAN BE EDITED.
                   else {
                       jQuery("#editButton").show();
                   }
                   
                },
                error : function() {
                    alert("fail in AJAX [admCheckDocument.php]");
                }
            }); // [END] AJAX IN CHECKING IF THIS DOCUMENT CAN BE EDITABLE.
            
            // [START] AJAX IN CHECKING IF THIS DOCUMENT CAN BE DELETED.
            // THIS MEANS THAT THE DOCUMENTIS NOT CONNECTED TO ANY ROUTE.
            jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/admCheckDocumentDelete.php',
                dataType : 'json',
                async : false,
                data: {
                    documentSetupId : "<?php echo $documentSetupId; ?>",
                    docType : "<?php echo $documentType; ?>"
                },
                success : function(data){
                   
                   // IF DATA/"1" = DOCUMENT HAS ON GOING TRANSACTIONS
                   if (data) {
                       jQuery("#deleteButton").hide();
                   }
                   
                   // ELSE = DOCUMENT HAS NO ON GOING TRANSACTIONS, IT CAN BE EDITED.
                   else {
                       jQuery("#deleteButton").show();
                   }
                   
                },
                error : function() {
                    alert("fail in AJAX [admCheckDocumentDelete.php]");
                }
            }); // [END] AJAX IN CHECKING IF THIS DOCUMENT CAN BE EDITABLE.
            
            // [START] EDIT BUTTON CLICKED > submit documentEditForm and redirect user to admDocumentEdit.php
            jQuery("#editButton").click(function() {
                tempDocId = "<?php echo $documentSetupId; ?>";
                
                // SAVE PASSED USER ID
                jQuery("#passedDocSetupId").val(tempDocId);

                // SUBMIT FORM FOR EDIT
                jQuery("#documentEditForm").submit();
            });
            
            // [START] DELETE BUTTON CLICKED > AJAX admDeleteDocument.php
            jQuery("#deleteButton").click(function() {
                tempUserId = "<?php echo $documentSetupId; ?>";
                
                // admDeleteDocument.php AJAX which deletes the document record in the database.
                jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/admDeleteDocument.php',
                    dataType : 'json',
                    async : false,
                    data: {
                        documentSetupId : "<?php echo $documentSetupId; ?>"
                    },
                    success : function(data){
                        
                        // SAVE DELETE MESSAGE
                        jQuery("#deleteMsg").val("You successfully removed <?php echo $documentName; ?>.");

                        // SUBMIT FORM FOR DELETE
                        jQuery("#documentDeleteForm").submit();
                    },
                    error : function() {
                        alert("fail in AJAX [admDeleteDocument.php]");
                    }
                }); // [END] AJAX IN CHECKING IF THIS DOCUMENT CAN BE EDITABLE.
                
                
            });
            
            
        }); // END - (DOCUMENT READY FUNCTION)
        
        
    </script>
    <!-- HEAD END -->


<?php include("siteMiddle.php"); ?>
    
<!-- START OF BODY -->
<div id="content">
    <h1>Document View Page</h1>
    <form>
        <span class="screenlet" >Document Information</span>
        <table border="1">
            <tr>
                <td>Document Setup Id</td>
                <td><?php echo $documentSetupId; ?></td>
            </tr>
            <tr>
                <td>Document Name</td>
                <td><?php echo $documentName; ?></td>
            </tr> 
            <tr>
                <td>Type</td>
                <td><?php echo $documentType; ?></td>
            </tr>
            <tr>
                <td>Description</td>
                <td><?php echo $documentDescription; ?></td>
            </tr>
        </table>
        
        <!-- MESSAGE IF DOCUMENT CANNOT BE EDITED -->
        <span id="cannotEditMsg" style="display:none;">This document cannot be modified<br>because it has on-going transactions.<br><br></span>
        
        
    <!-- EDIT BUTTON -->
    <input type="Button" name="editButton" id="editButton" class="siteGreenButton" value="Edit" style="display:none;"/>
   
    <!-- EDIT BUTTON -->
    <input type="Button" name="deleteButton" id="deleteButton" class="siteRedButton" value="Delete" style="display:none;"/>
    
    <!-- BACK BUTTON -->
    <input type="Button" name="backButton" id="backButton" class="siteGreenButton" value="Back"/>
    </form>
    
    <!-- THIS WILL SERVE AS THE FORM IN ORDER TO PASS documentSetupId TO admDocumentEdit.php -->    
    <form id="documentEditForm" action="admDocumentEdit.php" method="POST" target="_self">
        <input type="hidden" name="documentSetupId" id="passedDocSetupId"value=""/>
    </form>
    
    <!-- THIS WILL SERVE AS THE FORM IN ORDER TO PASS DELETE MESSAGE TO admDocumentSetup.php -->    
    <form id="documentDeleteForm" action="admDocumentSetup.php" method="POST" target="_self">
        <input type="hidden" name="docSuccessMessage" id="deleteMsg"value=""/>
    </form>

</div> 

<!-- BODY END -->
<?php 
    include ("siteBottom.php"); 
?>